package com.uzhie.web.service;

import com.uzhie.exception.RoleException;
import com.uzhie.util.ConstantKit;
import com.uzhie.web.bean.PageBean;
import com.uzhie.web.dao.RoleDAO;
import com.uzhie.web.dao.RoleMenuDAO;
import com.uzhie.web.module.Menu;
import com.uzhie.web.module.Role;
import com.uzhie.web.module.RoleMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 用户service,封装与用户相关的服务操作
 * <p>
 * Created by lilongfei on 2018/7/12.
 */
@Service
public class RoleService {

    @Autowired
    private RoleDAO roleDAO;

    @Autowired
    private RoleMenuDAO roleMenuDAO;


    /**
     * 根据条件查询角色列表
     *
     * @param name          角色名称
     * @param createName    创建人
     * @param minCreateTime 创建时间(范围查询)
     * @param maxCreateTime 创建时间(范围查询)
     * @param menuName      菜单名称
     * @return
     * @Param level 当前登录用户从属角色的等级
     */
    public List<Role> selectAllRoleByCondition(String name, String createName, String minCreateTime, String maxCreateTime, String menuName, Integer level, PageBean pageBean) {
        List<Role> list = roleDAO.selectAllRoleByCondition(name, createName, minCreateTime, maxCreateTime, menuName, level, pageBean.getRowBounds());
        return list;
    }


    /**
     * 添加角色
     *
     * @param role 角色对象
     * @throws RoleException 角色业务异常
     */
    public void addRole(@NotNull(message = RoleException.Status.ROLE_ISNULL) @Valid Role role) throws RoleException {

        //保存数据库
        role.setCreateTime(new Date());
        role.setStatus(ConstantKit.STATUS.IS_AVAILABLE);
        roleDAO.insert(role);
        //判断是否为空
        if (role.getId() == null) throw new RoleException(RoleException.Status.INSERT_ERROR);
        //判断是否有选择菜单数据
        if (role.getMenus() == null || role.getMenus().size() == 0)
            throw new RoleException(RoleException.Status.MENU_ISNULL);

        //遍历存储中间表
        for (Menu menu : role.getMenus()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role.getId());
            roleMenu.setMenuId(menu.getId());
            roleMenuDAO.insert(roleMenu);
        }
    }

    /**
     * 根据角色ID获取角色详情
     *
     * @param roleId
     * @return
     */
    public Role selectRoleById(String roleId) throws RoleException {
        if (roleId == null || roleId.equals("")) throw new RoleException(RoleException.Status.ID_ISNULL);
        Role role = roleDAO.selectRoleById(roleId);
        return role;
    }

    /**
     * 更新角色信息
     *
     * @param role 角色基本信息
     * @throws RoleException
     */
    public void updateRole(Role role) throws RoleException {

        //更新数据库
        role.setUpdateTime(new Date());
        int t = roleDAO.updateByPrimaryKeySelective(role);
        if (t != 1) throw new RoleException(RoleException.Status.UPDATA_ERROR);
        //删除角色菜单中间表
        t = roleMenuDAO.deleteAllByRoleId(role.getId());
        if (t == 0) throw new RoleException(RoleException.Status.DELETE_MENU_ERROR);
        //遍历存储中间表
        for (Menu menu : role.getMenus()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role.getId());
            roleMenu.setMenuId(menu.getId());
            roleMenuDAO.insert(roleMenu);
        }
    }

    /**
     * 根据角色id,批量修改角色信息数据库状态
     *
     * @param roleIds 角色id列表
     * @param status
     * @throws RoleException
     */
    public void updateRoleStatusById(List<Role> roleIds, Integer status) throws RoleException {

        if (roleIds.size() == 0) throw new RoleException(RoleException.Status.ID_ISNULL);
        for (Role role : roleIds) {
            int t = roleDAO.updateRoleStatusById(role.getId(), status);
            if (t == 0) throw new RoleException(RoleException.Status.UPDATA_STATUS_ERROR);
        }

    }

}


